﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.NRBZ;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.DataAccess.Common;

namespace Rison.Web.NRBZ
{
    public partial class ZfbsDetail : NrfbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public override void LoadInformation(DataRow dr)
        {
            if (dr != null)
            {
                string _xzspsxNum = dr["xzspsxNum"].ToString();
                //SetTextBoxValue(_xzspsxNum, "xzspsxNum");
                this.txtxzspsxNum1.Text = _xzspsxNum;

                string _zffwsxNum = dr["zffwsxNum"].ToString();
                //SetTextBoxValue(_zffwsxNum, "zffwsxNum");
                //this.txtzffwsxNum1.Text = _zffwsxNum;
                this.rblzffwsx.SelectedValue = dr["IFzffwsx"].ToString();

                //this.txtcgalNum.Text = dr["cgalNum"].ToString();
            }
            NrbzFacade facade = new NrbzFacade();

            DataSet ds = facade.LoadAttachement(NrbzId, 10);
            DataList10.DataSource = ds.Tables[0];
            DataList10.DataBind();
            //htmlTable
            LoadHtmlTable(NrbzSubType.NrbzSubType31.ToString(), "ZfbsDetail1_inputUrl", "tableUrl", 4);
            LoadHtmlTable(NrbzSubType.NrbzSubType32.ToString(), "ZfbsDetail1_sx", "tablesx", 4);
            //LoadHtmlTable(NrbzSubType.NrbzSubType33.ToString(), "ZfbsDetail1_inputNr", "tableNr", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType34.ToString(), "ZfbsDetail1_cgnl", "tablecgnl", 3);
        }

        public override long SaveInformation()
        {
            NrbzFacade facade = new NrbzFacade();
            List<DataField> l = new List<DataField>();

            int returnValue = 0;
            int num = 0;

            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form["ZfbsDetail1_inputUrl"]))
            {
                num= Convert.ToInt16(HttpContext.Current.Request.Form["ZfbsDetail1_inputUrl"].Split(',').Length) / 3;
               
            }
            else
            {
                num = 0;
            }

            num = num + facade.GetSubInfoCountbyShow(NrbzId, NrbzSubType.NrbzSubType31.ToString(), 3);
            this.txtxzspsxNum1.Text = num.ToString();

            //l.Add(new DataField("xzspsxNum",this.txtxzspsxNum1.Text));
            //l.Add(new DataField("zffwsxNum", this.txtzffwsxNum1.Text));
            //l.Add(new DataField("cgalNum",this.txtcgalNum.Text));
            l.Add(new DataField("IFzffwsx", this.rblzffwsx.SelectedValue));

           
            returnValue = facade.updateValue(l, NrbzId);

            //htmlTable
            string returnSubValue = "";
            returnSubValue = SaveHtmlTabel("ZfbsDetail1_inputUrl", NrbzSubType.NrbzSubType31.ToString(), 4);
            returnSubValue = SaveHtmlTabel("ZfbsDetail1_sx", NrbzSubType.NrbzSubType32.ToString(), 4);
            //returnSubValue = SaveHtmlTabel("ZfbsDetail1_inputNr", NrbzSubType.NrbzSubType33.ToString(), 2);
            returnSubValue = SaveHtmlTabel("ZfbsDetail1_cgnl", NrbzSubType.NrbzSubType34.ToString(), 3);

            uploadFile();
            return returnValue;
        }

        #region "textbox"
        protected void SetTextBoxValue(string value, string field)
        {
            string[] s = value.Split(',');
            for (int i = 0; i < s.Length; i++)
            {
                ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text = s[i].ToString();
            }
        }

        protected string GetTextBoxValue(string field, int num)
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                if (i == num - 1)
                {
                    s.AppendFormat("{0}", ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text);
                }
                else
                {
                    s.AppendFormat("{0},", ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text);
                }
            }
            return s.ToString();
        }
        #endregion

        #region "子网站 create/delete/update htmlTable"
        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="field">ZfgkDetail1_input_5_1</param>
        /// <returns></returns>
        private string SaveHtmlTabel(string field, string Type, int fieldNum)
        {
            string info = HttpContext.Current.Request.Form[field];
            NrbzFacade facade = new NrbzFacade();

            if (!string.IsNullOrEmpty(info))
            {               
                string msg = facade.SaveSubInfobyParentId(NrbzId, Type, fieldNum, info);
                return msg;
            }
            else
            {
                int returnVal = facade.DeleteSubInfobyParentId(NrbzId, Type);
                if (returnVal < 0) return "删除有误！";
            }
            return "";

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="tablename">ZfgkDetail1_table5_1</param>
        /// <param name="fieldname">ZfgkDetail1_input_5_1</param>

        private void LoadHtmlTable(string Type, string fieldname, string tablename, int fieldNum)
        {
            NrbzFacade facade = new NrbzFacade();
            DataTable dt = facade.LoadSubInfobyParentId(NrbzId, Type, fieldNum);
            if (dt == null || dt.Rows.Count == 0)
            {
                DrawOneRow(tablename, fieldname, "","","","",""); //blank row                
            }
            else
            {

                foreach (DataRow dr in dt.Rows)
                {
                    if(fieldNum == 2)
                    {
                        DrawOneRow(tablename, fieldname, dr[0].ToString(),dr[1].ToString(),"","","");
                    }
                    else if (fieldNum == 3)
                    {
                        DrawOneRow(tablename, fieldname, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(),"", dr["ifShow"].ToString());
                    }
                    else if (fieldNum == 4)
                    {
                        DrawOneRow(tablename, fieldname, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr["ifShow"].ToString());
                    }
                    else
                    {  
                        DrawOneRow(tablename, fieldname, dr[0].ToString(),"","","","");
                    }
                }

            }
        }

        private void DrawOneRow(string t, string inputname, string p1,string p2,string p3,string p4,string isshow)
        {
            if (t == "tableUrl")
            {
                DrawOneRow1(t, inputname, p1, p2, p3, p4, isshow);
            }
            else if (t == "tablesx")
            {
                //DrawOneRow2(t, inputname, p1, p2, p3, p4);
            }
            else if (t == "tableNr")
            {
                DrawOneRow3(t, inputname, p1,p2);
            }
            else if (t == "tablecgnl")
            {
               // DrawOneRow4(t,inputname, p1,p2,p3);
            }

        }

        private void DrawOneRow1(string t, string inputname, string p1, string p2, string p3, string p4, string isshow)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            isshow = "readonly='readonly'"; 

            ce.InnerHtml = "事项名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:250px;' class='Text' " + isshow + "/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "责任科室 <input type='text' name='" + inputname + "' value='" + p2 + "' style='width:150px;' class='Text' />";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "相关表格 <input type='text' name='" + inputname + "' value='" + p3 + "' style='width:50px;' class='Text' /> 张";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "2011年在线受理数量 <input type='text' name='" + inputname + "' value='" + p4 + "' style='width:50px;' class='Text' " + isshow + "/> 次";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

        private void DrawOneRow2(string t, string inputname, string p1, string p2, string p3, string p4)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "事项名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:150px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "是否已经公开发布 <input type='radio' name='" + inputname + "' value='是'  checked='checked' />是 ";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<input type='radio' name='" + inputname + "' value='否' />否 ";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "&nbsp;&nbsp;相关表格 <input type='text' name='" + inputname + "' value='" + p3 + "' style='width:60px;' class='Text'/> 张";
            tr.Cells.Add(ce);

            if (p4 == "") p4 = "http://";

            ce = new HtmlTableCell();
            ce.InnerHtml = "链接地址 <input type='text' name='" + inputname + "' value='" + p4 + "' style='width:180px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }       

        private void DrawOneRow3(string t, string inputname, string p1,string p2)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "内容 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:200px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "URL <input type='text' name='" + inputname + "' value='" + p2 + "' style='width:300px;' class='Text'/>";
            tr.Cells.Add(ce);
            
            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

        private void DrawOneRow4(string t, string inputname, string p1, string p2, string p3)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "事项名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:250px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "本年受理人次 <input type='text' name='" + inputname + "' value='" + p2 + "' style='width:100px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "占事项年受理人次的百分比 <input type='text' name='" + inputname + "' value='" + p3 + "' style='width:100px;' class='Text'/> %";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

        #endregion

        #region "upload"
        public string uploadFile()
        {
            string val = Request.Params["fileUploader10_hdnFileVal"];
            List<SysAttachment> _attachments = new List<SysAttachment>();
            int returnValue = -1;
            string del = "";

            NrbzFacade facade = new NrbzFacade();

            //附件10
            if (!string.IsNullOrEmpty(val))
            {
                _attachments = NewUpload(val);
                returnValue = facade.InsertAttachment(NrbzId, 10, _attachments);
            }

            del = Request.Params["fileUploader10_del"];
            if (!string.IsNullOrEmpty(del))
                returnValue = facade.DeleteAttachment(del);


            if (returnValue < 0)
            {
                return "文件上传成功，保存有误！";
            }

            return "";
        }
        #endregion
    }
}